#!/bin/bash
# author tyb
# date 20240222

# 执行结果判断函数
function isgreen() {
    if [ $? -eq 0 ]; then
        echo -e "\e[32m$1==========>succeed\e[0m"
    else
        echo -e "\e[31m$1==========>failed\e[0m"
        # exit 1
    fi
}

# 网络测试函数
function network() {
    #超时时间
    local timeout=1

    #目标网站
    local target=www.baidu.com

    #获取响应状态码
    local ret_code
    ret_code=$(curl -I -s --connect-timeout ${timeout} ${target} -w "%{http_code}" | tail -n1)

    if [ "$ret_code" = "200" ]; then
        #网络畅通
        echo -e "\e[32m网络畅通==========>succeed\e[0m"
        return 0
    else
        #网络不畅通
        echo -e "\e[31m网络不畅通，请检查网络==========>succeed\e[0m"
        exit 1
    fi

}

function print_help() {
  echo "Usage: $0 [OPTIONS]"
  echo "Options:"
  echo "  --help            Display this help message."
  
  echo "请将以下包放入/opt/目录下"
  echo "    cloudstack-common_4.18.0.0~jammy~jammy_all.deb"
  echo "    cloudstack-agent_4.18.0.0~jammy~jammy_all.deb"
  echo "    managerbdvmsnap.py"
  echo "    kvmheartbeat.sh"
  echo "    createrbdvmsnap.py"
  echo "    cephextop.sh"
  echo "请将以下包放入/usr/bin/目录下"
  echo "    cloud-agent-utils"
}

# 收集变量
while [[ $# -gt 0 ]]; do
  case "$1" in
    --help)
       print_help
        exit 0
        ;;
    *)
      echo "Invalid option: $1"
      exit 1
      ;;
  esac
done

# 获取cloudstack-common_4.18.0.0~jammy~jammy_all.deb和cloudstack-agent_4.18.0.0~jammy~jammy_all.deb包
pkg1="cloudstack-common_4.18.0.0~jammy~jammy_all.deb"
pkg2="cloudstack-agent_4.18.0.0~jammy~jammy_all.deb"
agent_file="cloud-agent-utils"
files="managerbdvmsnap.py kvmheartbeat.sh createrbdvmsnap.py cephextop.sh"
# 检查文件是否存在
if ! [ -e "/opt/$pkg1" ] && ! [ -e "/opt/$pkg2" ]; then
    echo -e "\e[31m请将 $pkg1 和 $pkg2 放在opt目录下\e[0m"
    exit
fi

if ! [ -e "/usr/bin/$agent_file" ]; then
    echo -e "\e[31m请将 $agent_file 放在 /usr/bin 目录下\e[0m"
    exit
fi

for file in $files
do
    if ! [ -e "/opt/$file" ];then
        echo -e "\e[31m请将 $file 放在 /opt 目录下\e[0m"
        exit
    fi
done

echo "安装软件"
network
code=$?
if [ $code -eq 0 ]; then
    echo "更新apt源"
    sudo apt-get -y update >/dev/null 2>&1
    isgreen "更新apt源"

    # 安装软件
    echo "安装acs代理"
    apt install -y /opt/cloudstack-agent_4.18.0.0~jammy~jammy_all.deb /opt/cloudstack-common_4.18.0.0~jammy~jammy_all.deb >/dev/null 2>&1
    isgreen "安装acs代理"

    echo "安装依赖和一些工具"
    apt install -y libvirt-daemon-driver-storage-rbd libvirt-daemon-driver-lxc virt-manager virt-top lrzsz net-tools sysstat  smartmontools >/dev/null 2>&1
    isgreen "安装依赖和一些工具"
fi



# 修改配置文件
echo "修改 libvirtd.conf 配置文件"
if ! [ -e /etc/libvirt/libvirtd.conf.bak ]; then
    cp /etc/libvirt/libvirtd.conf /etc/libvirt/libvirtd.conf.bak >/dev/null 2>&1
    isgreen "备份 /etc/libvirt/libvirtd.conf"

    cat << EOF >> /etc/libvirt/libvirtd.conf
listen_tls = 0
listen_tcp = 0
tls_port = "16514"
tcp_port = "16509"
auth_tcp = "none"
mdns_adv = 0
EOF
isgreen "追加参数"
fi
isgreen "修改 libvirtd.conf 配置文件"

echo "修改 libvirtd 配置文件"
if ! [ -e "/etc/default/libvirtd.bak" ]; then
    mv /etc/default/libvirtd /etc/default/libvirtd.bak >/dev/null 2>&1
    isgreen "备份 /etc/default/libvirtd"

    echo "libvirtd_opts='-l'" > /etc/default/libvirtd
    isgreen "只保留一行参数"
fi
isgreen "修改 libvirtd 配置文件"

echo "重启libvirtd"
systemctl restart libvirtd >/dev/null 2>&1
isgreen "重启libvirtd"

echo "修改 agent.properties 配置文件"
if ! [ -e "/etc/cloudstack/agent/agent.properties.bak" ]; then
    cp /etc/cloudstack/agent/agent.properties /etc/cloudstack/agent/agent.properties.bak
    isgreen "备份 /etc/cloudstack/agent/agent.properties"

    cat << EOF >> /etc/cloudstack/agent/agent.properties
guest.cpu.mode=host-passthrough
guest.cpu.features=vmx
vm.video.ram=16384
vm.video.hardware=qxl
host.reserved.mem.mb=32768
vm.memballoon.disable=true
EOF
isgreen "追加参数"
fi
isgreen "修改 agent.properties 配置文件"

echo "启用UEFI支持"
if ! [ -e "/etc/cloudstack/agent/uefi.properties" ]; then
    cat << EOF >> /etc/cloudstack/agent/uefi.properties
guest.nvram.template.secure=/usr/share/OVMF/OVMF_VARS.ms.fd
guest.nvram.template.legacy=/usr/share/OVMF/OVMF_VARS.fd
guest.loader.secure=/usr/share/OVMF/OVMF_CODE.secboot.fd
guest.nvram.path=/var/lib/libvirt/qemu/nvram/
guest.loader.legacy=/usr/share/OVMF/OVMF_CODE.fd
EOF
isgreen "添加配置文件"
fi
isgreen "启用UEFI支持"

echo "新建服务"
if ! [ -e "/lib/systemd/system/cloud-agent-utils.service" ]; then
    cat << EOF >> /lib/systemd/system/cloud-agent-utils.service
[Unit]
Description=Cloud Agent Utils Service
 
[Service]
User=root
Type=simple
ExecStart=/usr/bin/cloud-agent-utils
Restart=on-failure
RestartSec=10
 
[Install]
WantedBy=multi-user.target
EOF
isgreen "添加配置文件"
fi
isgreen "新建服务"

echo "为$agent_file添加权限"
chmod +x /usr/bin/"$agent_file"
isgreen "为$agent_file添加权限"

echo "启动并激活服务"
systemctl enable cloud-agent-utils && systemctl start cloud-agent-utils >/dev/null 2>&1
isgreen "启动并激活服务"


echo "添加脚本文件"
for file in $files
do
    if ! [ -e "/usr/share/cloudstack-common/scripts/vm/hypervisor/kvm/$file.bak" ]; then
        mv /usr/share/cloudstack-common/scripts/vm/hypervisor/kvm/"$file" /usr/share/cloudstack-common/scripts/vm/hypervisor/kvm/"$file".bak
        isgreen "备份$file"
        mv /opt/"$file" /usr/share/cloudstack-common/scripts/vm/hypervisor/kvm/
        isgreen "添加新的$file"
    fi
done
isgreen "添加脚本文件"

echo "为脚本文件添加执行权限"
chmod +x  /usr/share/cloudstack-common/scripts/vm/hypervisor/kvm/* >/dev/null 2>&1
isgreen "为脚本文件添加执行权限"


# echo "请将 $files 复制到/usr/share/cloudstack-common/scripts/vm/hypervisor/kvm"
# echo "为所有脚本添加权限：chmod +x  /usr/share/cloudstack-common/scripts/vm/hypervisor/kvm/*"
